'use strict';
import qiniu from 'qiniu';
export default class extends think.service.base {
    /**
     * init
     * @return {}         []
     */


    async uploadFile(uptoken, key, localFile, config) {
        var extra = new qiniu.io.PutExtra();
        return new Promise(function(resolve, reject) {
            qiniu.io.putFile(uptoken, key, localFile, extra, function(err, ret) {
                if (!err) {
                    let imgName = ret.key;
                    let imgUrl = 'http://'+config.domain + '/' + imgName;
                    let model = {
                        merchantId: 1,
                        imgUrl: imgUrl,
                        text: '占位',
                        imgType: '占位silde banner'
                    }

                    resolve(imgUrl)
                } else {
                    reject(new Error())
                }
            });
        });
    }
    uptoken(bucket, key) {
        var putPolicy = new qiniu.rs.PutPolicy(bucket + ":" + key);
        return putPolicy.token();
    }

    create() {
        let Num = 'qwertyuiopasdfghjklzxcvbnm123456789';
        let resulet = '';
        for (let i = 0; i < 8; i++) {
            let n = Math.floor(Math.random() * 30);
            resulet += Num[n];
        }
        return resulet;
    }
}
